/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookingsystem;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author admin
 */
public class Main {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) throws IOException {
        ServerSocket server = null;
        int port = 7777;
        String backupServerIp = "127.0.0.1";
        try {
            DBConnection conn = new DBConnection();
            String sid = "centraldb";
            Connection centralConnection = conn.getConnection(sid,"127.0.0.1");
            Connection backupConnection = conn.getConnection("backupdb",backupServerIp);
            Statement central = centralConnection.createStatement();
            Statement backup = backupConnection.createStatement();
            ResultSet rset;
            String idreservation = "";
            String reservationId = "";
            String origin = "";
            String destination = "";
            String flightDate = "";
            String bookingDate = "";
            String custName = "";
            String custLastName = "";
            String status = "";
            int flightNo = 0;
            int sequence = 0;
            rset = central.executeQuery("Select sequence from Reservation order by sequence desc limit 1");
            while (rset.next()){
                sequence = (int) Integer.parseInt(rset.getString(10));
            }
            rset.beforeFirst();
            rset = backup.executeQuery("Select * from Reservation where sequence > '"+sequence+"'");
            while (rset.next()){
                reservationId = rset.getString(1);
                origin = rset.getString(2);
                destination = rset.getString(3);
                flightDate = rset.getString(4);
                flightNo = (int) Integer.parseInt(rset.getString(5));
                bookingDate = rset.getString(6);
                custName = rset.getString(7);
                custLastName = rset.getString(8);
                status = rset.getString(9);
                sequence = (int) Integer.parseInt(rset.getString(10));
                central.execute("Insert into reservation values ('"+reservationId+"','"+origin+"','"+destination+"','"+flightDate+"','"+flightNo+"','"+bookingDate+"','"+custName+"','"+custLastName+"','"+status+"','"+sequence+"')");
            }
            
            // do the same for queue table
            rset = central.executeQuery("Select sequence from queue order by sequence desc limit 1");
            while (rset.next()){
                sequence = (int) Integer.parseInt(rset.getString(10));
            }
            rset.beforeFirst();
            rset = backup.executeQuery("Select * from queue where sequence > '"+sequence+"'");
            while (rset.next()){
                reservationId = rset.getString(1);
                origin = rset.getString(2);
                destination = rset.getString(3);
                flightDate = rset.getString(4);
                flightNo = (int) Integer.parseInt(rset.getString(5));
                bookingDate = rset.getString(6);
                custName = rset.getString(7);
                custLastName = rset.getString(8);
                status = rset.getString(9);
                sequence = (int) Integer.parseInt(rset.getString(10));
                central.execute("Insert into queue values ('"+reservationId+"','"+origin+"','"+destination+"','"+flightDate+"','"+flightNo+"','"+bookingDate+"','"+custName+"','"+custLastName+"','"+status+"','"+sequence+")");
            }
        }
        catch (SQLException e) { System.out.println("Conn failed: "+e);// Could not connect to the database
        }
        Socket client = null;
        try{
            while(true){
            ServerThread st;
            try {
                System.out.println("Waiting for client");
                server = new ServerSocket(port);
                st = new ServerThread(server.accept());
                Thread t = new Thread(st);
                t.start();
            } catch (IOException e) {
                System.err.println("Accept failed.");
                System.exit(1);
            }
            server.close();
            }
        }
        catch (IOException e){
            System.out.println(e);
        }
    }
}
